package com.example.javaui.slice;

import com.example.javaui.ResourceTable;
import com.example.javaui.Utils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.*;

import java.time.Duration;

import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_CONTENT;
import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_PARENT;

public class DialogAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_slice_dialog);

        Button btnToastDialog = (Button) findComponentById(ResourceTable.Id_btn_toastdialog);
        btnToastDialog.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                /*
                ToastDialog toastDialog = new ToastDialog(DialogAbilitySlice.this);
                toastDialog.setText("这是一个ToastDialog").show();
                */
                Utils.showToast(DialogAbilitySlice.this, "这是一个ToastDialog");
            }
        });

        Button btnListDialog = (Button) findComponentById(ResourceTable.Id_btn_listdialog);
        btnListDialog.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                getUITaskDispatcher().asyncDispatch(new Runnable() {
                    @Override
                    public void run() {

                        ListDialog listDialog = new ListDialog(DialogAbilitySlice.this, ListDialog.NORMAL);
                        listDialog.setItems(new String[]{"第1个选项", "第2个选项"});
                        // listDialog.setSingleSelectItems(new String[]{"第1个选项", "第2个选项"}, 0);
                        // listDialog.setMultiSelectItems(new String[]{"第1个选项", "第2个选项"}, new boolean[]{true, false} );
                        listDialog.setListener(new ListContainer.ItemClickedListener() {
                            @Override
                            public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                                Utils.showToast(DialogAbilitySlice.this, "单击了第" + (i + 1) + "个选项");
                            }
                        }, null, null);
                        listDialog.setButton(0, "取消", new IDialog.ClickedListener() {
                            @Override
                            public void onClick(IDialog iDialog, int i) {
                                listDialog.hide();
                            }
                        });
                        listDialog.setButton(1, "确认", null);
                        listDialog.setSize(400, 300);
                        listDialog.show();
                    }
                });

            }
        });


        Button btnPopupDialog = (Button) findComponentById(ResourceTable.Id_btn_popupdialog);
        btnPopupDialog.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                // 第2个参数为锚点位置，第3、4个参数分别为宽度和高度
                PopupDialog popupDialog = new PopupDialog(DialogAbilitySlice.this, btnPopupDialog, 600, 100);
                popupDialog.setBackColor(Color.CYAN);  // 背景颜色
                popupDialog.setHasArrow(true);  // 是否显示气泡箭头
                // 创建自定义DialogBox组件
                Text text = new Text(DialogAbilitySlice.this);
                text.setText("这是一个PopupDialog!");
                text.setTextSize(50);
                text.setLayoutConfig(new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT, ComponentContainer.LayoutConfig.MATCH_CONTENT));
                text.setClickedListener(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        popupDialog.hide();
                    }
                });
                popupDialog.setCustomComponent(text).setMode(PopupDialog.ICON2).show();

            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
